package com.zzld.modules.autoinn.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzld.core.service.BaseServiceImpl;
import com.zzld.modules.autoinn.entity.Area;
import com.zzld.modules.autoinn.mapper.AreaMapper;
import com.zzld.modules.autoinn.service.IAreaService;
import com.zzld.modules.common.dto.TreeGrid;

@Service("areaService")
public class AreaServiceImpl  extends BaseServiceImpl<Area> implements IAreaService{

	@Autowired
	private AreaMapper areaMapper;
	
	
	public Integer sercount(String pid){
		return areaMapper.sercount(pid);
	}
	
	/**
	 * 根据id查询一条数据
	 * @param id
	 * @return
	 */
	public Area selectbyid(String id){
		 return areaMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 新增或修改
	 * @param area
	 * @param i
	 * @return
	 */
	public int uporadd(Area area,int i){
		try{
			if(i==0){ //新增
				return (Integer) areaMapper.insertSelective(area);
			}else{
				return (Integer) areaMapper.updateByPrimaryKeySelective(area);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 获取树形网格
	 * @return
	 */
	public List<TreeGrid> treeGrid(){
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		List<Area> listRes= areaMapper.treegrid();
		if(listRes !=null && listRes.size()>0){
			for (Area res : listRes) {
				TreeGrid treeGrid=new TreeGrid();
				BeanUtils.copyProperties(res, treeGrid);
				treeGrid.setText(res.getAreaname());
				treeGrid.setCreateBy(res.getCreate_by());
				treeGrid.setCreateDate(res.getCreate_date());
				treeGrids.add(treeGrid);
			}
		}
		return treeGrids;
	}
	
	/**
	 * 删除操作
	 * @param id
	 * @return
	 */
	public int deletearea(String id){
		return areaMapper.deleteByPrimaryKey(id);
	}
}
